<?php

class SimpleCurl
{
	public $response;
	public $size;
	public $httpCode;
	
	public $error;
	public $info;
	
	public function httpRequest($url, $method, $data = '', $opts = false) {
			$this->response = '';
			$this->size = 0;
			$this->httpCode = null;
			$this->requestContent = null;
			
			$defaults = array( 
	        				CURLOPT_HEADER => false, 
	        				CURLOPT_URL => $url, 
	        				CURLOPT_RETURNTRANSFER => true, 
	        				CURLOPT_WRITEFUNCTION => array($this, "receiveData")
	    					);
	    	//build query if data array given				
	    	if(is_array($data))
						$data = http_build_query($data);
			if(!$data) $data = '';
	    					
			switch($method)
			{
				case 'get':
					$def = array( 
	        						CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : '') . $data,  
	        						CURLOPT_TIMEOUT => 4 
	    						);
	    			$defaults = $defaults + $def; 
					break;
				case 'post':
					$def = array(
									CURLOPT_POST => 1,
									CURLOPT_FRESH_CONNECT => 1, 
									CURLOPT_FORBID_REUSE => 1, 
	        						CURLOPT_TIMEOUT => 4, 
					 				CURLOPT_POSTFIELDS => $data
					 			);
					$defaults = $defaults + $def;
			  		break;
				case 'put':
					$content = (string) "Content-Length: ".strlen($data);
					$def = array(
									//CURLOPT_PUT => true
									CURLOPT_CUSTOMREQUEST => 'PUT',
									CURLOPT_HTTPHEADER => array($content),
									CURLOPT_POSTFIELDS => (string) $data
						 		);
					$defaults = $defaults + $def;
					//var_dump($defaults);
					break;
				case 'delete':
					$def = array(
									CURLOPT_CUSTOMREQUEST => 'DELETE'
						 		);
					$defaults = $defaults + $def;
					break;
			}
			
			$ch = curl_init($url);

			curl_setopt_array($ch, $defaults);
			if($opts)
				curl_setopt_array($ch, $opts);
				
			curl_exec($ch);
			
			$this->error = $err = curl_error($ch);
			$this->info = curl_getinfo($ch);
			
			if(($this->httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE)) != 200)
				 $e = true;
			else $e = false;
					
			curl_close($ch);
			
			if($err || $e)
				return false;
			return $this->response;
		}
		
		private function receiveData($ch, $data)
        {
			$len = strlen($data);
			$this->response .=  $data;
			$this->size += $len;
			return $len;
        }
}
?>